package com.example.team.dao.Imp;

import com.example.team.dao.IBusinesstripDao;
import com.example.team.entity.Businesstrip;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public class IBusinesstripDaoImpl implements IBusinesstripDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    //添加方法
    @Override
    public int insertBusinesstrip(Businesstrip businesstrip) {
        return jdbcTemplate.update("insert into businesstrip(TripID,EmployeeID,StartDate,EndDate,Description,Ratifiers)"+
                "values(?,?,?,?,?,?)"
                ,businesstrip.getTripID()
                ,businesstrip.getEmployeeID()
                ,businesstrip.getStartDate()
                ,businesstrip.getEndDate()
                ,businesstrip.getDescription()
                ,businesstrip.getRatifiers());
    }

    @Override
    public int deleteBusinesstrip(Businesstrip businesstrip) {
        return jdbcTemplate.update("delete from businesstrip where TripID=?",businesstrip.getTripID());
    }

    @Override
    public int updateBusinesstrip(Businesstrip businesstrip) {
        return jdbcTemplate.update("update businesstrip set EmployeeID=?,StartDate=?,EndDate=?,Description=?,Ratifiers=? where TripID=?",
                businesstrip.getEmployeeID(),
                businesstrip.getStartDate(),
                businesstrip.getEndDate(),
                businesstrip.getDescription(),
                businesstrip.getRatifiers(),
                businesstrip.getTripID());
    }

    @Override
    public List<Businesstrip> getAllBusinesstrip(Businesstrip businesstrip) {
        return jdbcTemplate.query("select " +
                        "`TripID`,`EmployeeID`,`StartDate`,`EndDate`,Description,`Ratifiers` " +
                        "from businesstrip",new Object[]{},
                new BeanPropertyRowMapper<>(Businesstrip.class));
    }

    @Override
    public Businesstrip getBusinesstrip(Businesstrip businesstrip) {
        List<Businesstrip> result=jdbcTemplate.query("select " +
                        "`TripID`,`EmployeeID`,`StartDate`,`EndDate`,`Description`,`Ratifiers`" +
                        "from businesstrip where TripID='"+businesstrip.getTripID()+"'",
                new Object[]{},
                new BeanPropertyRowMapper<>(Businesstrip.class));
        if (result.size()>=1)
            return result.get(0);
        else return null;
    }

    @Override
    public int signBusinesstrip(Businesstrip businesstrip) {
        return jdbcTemplate.update(
                "update businesstrip set Ratifiers=?"+"WHERE TripID=?",
                businesstrip.getRatifiers(),businesstrip.getTripID()
        );
    }
}
